home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / drdobbs / 1989 / 01 / convtc.asc next >
Text File  |  1989-01-02  |  9KB  |  383 lines

  1. _C PROGRAMMING_
  2.  
  3. by Al Stevens
  4.  
  5. [LISTING ONE]
  6.  
  7.  
  8. /* -------------- microsft.h ---------------- */
  9. /* #include this file at the end of window.h */
  10.  
  11. #if COMPILER == MSOFT
  12.  
  13. /* this line replaces the select_window prototype in window.h */
  14. int select_window(int, int, int, int (*)(int,int));
  15.  
  16. #define setmem(bf,sz,c) memset(bf,c,sz)
  17. #define movmem(fr,to,ln) memmove(to,fr,ln)
  18. #define cprintf wprintf
  19. #define cputs(s) wprintf(s)
  20. #define putch(c) wputch(c)
  21. #define getch() wgetch()
  22.  
  23. void window(int lf,int tp,int rt,int bt);
  24. void puttext(int lf,int tp,int rt,int bt,char *sv);
  25. void gettext(int lf,int tp,int rt,int bt,char *sv);
  26. void movetext(int lf, int tp, int rt, int bt, int lf1, int tp1);
  27. void gotoxy(int x,int y);
  28. void textcolor(int cl);
  29. void textbackground(int cl);
  30. int  wherex(void);
  31. int  wherey(void);
  32. void wprintf(char *, ...);
  33. void wputch(int c);
  34. int wgetch(void);
  35.  
  36. #define BLACK         0
  37. #define BLUE          1
  38. #define GREEN         2
  39. #define CYAN          3
  40. #define RED           4
  41. #define MAGENTA       5
  42. #define BROWN         6
  43. #define LIGHTGRAY     7
  44. #define DARKGRAY      8
  45. #define LIGHTBLUE     9
  46. #define LIGHTGREEN   10
  47. #define LIGHTCYAN    11
  48. #define LIGHTRED     12
  49. #define LIGHTMAGENTA 13
  50. #define YELLOW       14
  51. #define WHITE        15
  52.  
  53. #endif
  54.  
  55.  
  56. [LISTING TWO]
  57.  
  58. /* ----------- microsft.c ------------ */
  59.  
  60. /*
  61.  *  Surrogate Turbo C functions
  62.  *  for Microsoft C users.
  63.  */
  64.  
  65. #include <dos.h>
  66. #include <string.h>
  67. #include <stdio.h>
  68. #include <stdarg.h>
  69. #include <ctype.h>
  70. #include <conio.h>
  71. #include <bios.h>
  72.  
  73. /* -------- One of these is your Display Adapter -------- */
  74. #define MDA 1           /* Monochrome Display Adapter */
  75. #define CGA 2           /* Color Graphics Adapter     */
  76. #define EGA 3           /* Enhanced Graphics Adapter  */
  77. #define VGA 4           /* Video Graphics Array       */
  78.  
  79. #define ADAPTER EGA     /* Specifies the Display Adapter */
  80.  
  81. #if ADAPTER==MDA
  82. #define VSEG 0xb000     /* VSEG is the video memory segment */
  83. #else
  84. #define VSEG 0xb800
  85. #endif
  86.  
  87. #if ADAPTER==CGA
  88. #define SNOW 1
  89. /* --- assembly language vpeek.asm: manages CGA flicker --- */
  90. void vpoke(unsigned adr, unsigned off, int ch);
  91. int vpeek(unsigned adr, unsigned off);
  92. #else
  93. #define SNOW 0
  94. /* ---- macros for vpeek and vpoke for non-CGA systems ---- */
  95. #define MKFP(s,o)    (((long)s<<16)|o)
  96. #define vpoke(a,b,c) (*((int  far*)MKFP(a,b))=c)
  97. #define vpeek(a,b)   (*((int  far*)MKFP(a,b)))
  98. #endif
  99.  
  100. static union REGS rg;
  101.  
  102. /* --- a structure defined within Turbo C and used by us --- */
  103. struct {
  104.     char filler1[4];
  105.     char attribute;     /* saves the current video attribute */
  106.     char filler2[5];
  107.     char snow;          /* says if the adapter snows */
  108. } _video;
  109.  
  110. static int wlf,wtp,wrt,wbt; /* current window corners */
  111. static int wx,wy;           /* current window cursor  */
  112.  
  113. /* ------- define a video window ---------- */
  114. void window(int lf,int tp,int rt,int bt)
  115. {
  116.     wlf = lf;
  117.     wtp = tp;
  118.     wrt = rt;
  119.     wbt = bt;
  120.     _video.snow = (char ) SNOW;
  121. }
  122.  
  123. /* ------ makes a video offset from x,y coordinates ----- */
  124. #define vaddr(x,y) (((y)-1)*160+((x)-1)*2)
  125.  
  126. /* -- makes far pointer to video RAM from x,y coordinates -- */
  127. void far * pascal __vptr(int x, int y)
  128. {
  129.     void far *vp;
  130.  
  131.     FP_SEG(vp) = VSEG;
  132.     FP_OFF(vp) = vaddr(x,y);
  133.     return vp;
  134. }
  135.  
  136. /* ---- writes a block of memory to video ram ----- */
  137. void pascal __vram(int far *vp, int far *bf, int len)
  138. {
  139.     while(len--)    {
  140.         vpoke(VSEG, FP_OFF(vp), *bf++);
  141.         vp++;
  142.     }
  143. }
  144.  
  145. /* ---- gets a block of memory from video ram ----- */
  146. void pascal __getvram(int far *vp, int far *bf, int len)
  147. {
  148.     while(len--)    {
  149.         *bf++ = vpeek(VSEG, FP_OFF(vp));
  150.         vp++;
  151.     }
  152. }
  153.  
  154. /* ----- writes a memory block to a video window ----- */
  155. void puttext(int lf,int tp,int rt,int bt,char *sv)
  156. {
  157.     while (tp < bt+1)   {
  158.         __vram(__vptr(lf, tp), (int far *) sv, rt+1-lf);
  159.         tp++;
  160.         sv += (rt+1-lf)*2;
  161.     }
  162. }
  163.  
  164. /* ----- reads a memory block from a video window ------ */
  165. void gettext(int lf,int tp,int rt,int bt,char *sv)
  166. {
  167.     while (tp < bt+1)   {
  168.         __getvram(__vptr(lf, tp), (int far *) sv, rt+1-lf);
  169.         tp++;
  170.         sv += (rt+1-lf)*2;
  171.     }
  172. }
  173.  
  174. /* ------ moves a video window (used for scrolling) ------ */
  175. void movetext(int lf, int tp, int rt, int bt, int lf1, int tp1)
  176. {
  177.     int nolines = bt - tp + 1;
  178.     int incr = tp - tp1;
  179.     int len, i;
  180.     unsigned src, dst;
  181.  
  182.     if (tp > tp1)   {
  183.         src = tp;
  184.         dst = tp1;
  185.     }
  186.     else    {
  187.         src = bt;
  188.         dst = tp1+nolines-1;
  189.     }
  190.     while (nolines--)   {
  191.         len = rt - lf + 1;
  192.         for (i = 0; i < len; i++)
  193.             vpoke(VSEG, vaddr(lf1+i, dst),
  194.                 vpeek(VSEG,vaddr(lf+i, src)));
  195.         src += incr;
  196.         dst += incr;
  197.     }
  198. }
  199.  
  200. /* ----- position the window cursor ------ */
  201. void gotoxy(int x,int y)
  202. {
  203.     wx = x;
  204.     wy = y;
  205.     rg.h.ah = 15;
  206.     int86(16, &rg, &rg);
  207.     rg.x.ax = 0x0200;
  208.     rg.h.dh = wtp + y - 2;
  209.     rg.h.dl = wlf + x - 2;
  210.     int86(16, &rg, &rg);
  211. }
  212.  
  213. /* ----- return the window cursor x coordinate ----- */
  214. int wherex(void)
  215. {
  216.     return wx;
  217. }
  218.  
  219. /* ----- return the window cursor y coordinate ----- */
  220. int wherey(void)
  221. {
  222.     return wy;
  223. }
  224.  
  225. /* ----- sets the window foreground (text) color ------- */
  226. void textcolor(int cl)
  227. {
  228.     _video.attribute = (_video.attribute & 0xf0) | (cl&0xf);
  229. }
  230.  
  231. /* ----- sets the window background color ------- */
  232. void textbackground(int cl)
  233. {
  234.     _video.attribute = (_video.attribute & 0x8f) | ((cl&7)<<4);
  235. }
  236.  
  237. void writeline(int, int, char *);
  238.  
  239. /* ------ our substitution for MSC cprintf -------- */
  240. void wprintf(char *ln, ...)
  241. {
  242.     char dlin [81], *dl = dlin, ch;
  243.     int cl[81], *cp = cl;
  244.     va_list ap;
  245.  
  246.     va_start(ap, ln);
  247.     vsprintf(dlin, ln, ap);
  248.     va_end(ap);
  249.  
  250.     while (*dl) {
  251.         ch = (*dl++ & 255);
  252.         if (!isprint(ch))
  253.             ch = ' ';
  254.         *cp++ = ch | (_video.attribute << 8);
  255.     }
  256.     __vram(__vptr(wx+wlf-1,wy+wtp-1),
  257.             (int far *) cl, strlen(dlin));
  258.     wx += strlen(dlin);
  259. }
  260.  
  261. /* ------ our substitution for MSC putch -------- */
  262. void wputch(c)
  263. {
  264.     if (!isprint(c))
  265.         putch(c);
  266.     wprintf("%c", c);
  267. }
  268.  
  269. /* ------ our substitution for MSC getch -------- */
  270. int wgetch(void)
  271. {
  272.     static unsigned ch = 0xffff;
  273.  
  274.     if ((ch & 0xff) == 0)   {
  275.         ch++;
  276.         return (ch >> 8) & 0x7f;
  277.     }
  278.     ch = _bios_keybrd(_KEYBRD_READ);
  279.     return ch & 0x7f;
  280. }
  281.  
  282.  
  283. [LISTING THREE]
  284.  
  285. ;---------------------------  vpeek.asm  ----------------------------
  286.         dosseg
  287.         .model compact
  288.         .code
  289.         public  _vpoke
  290. ; -------- insert a word into video memory
  291. ;   vpoke(vseg, adr, ch);
  292. ;   unsigned vseg;    /* the video segment address     */
  293. ;   unsigned adr;     /* the video offset address      */
  294. ;   unsigned ch;      /* display byte & attribute byte */
  295. ; ------------------------------------------
  296. _vpoke  proc
  297.         push    bp
  298.         mov     bp,sp
  299.         push    di
  300.         push    es
  301.         mov     cx,4[bp]    ; video board base address
  302.         mov     es,cx
  303.         mov     di,6[bp]    ; offset address from caller
  304.         mov     dx,986      ; video status port address
  305. loop1:  in      al,dx       ; wait for retrace to quit
  306.         test    al,1
  307.         jnz     loop1
  308. loop2:  in      al,dx       ; wait for retrace to start
  309.         test    al,1
  310.         jz      loop2
  311.         mov     ax,8[bp]    ; word to insert
  312.         stosw               ; insert it
  313.         pop     es
  314.         pop     di
  315.         pop     bp
  316.         ret
  317. _vpoke  endp
  318.  
  319.         public  _vpeek
  320. ; -------- retrieve a word from video memory
  321. ;   vpeek(vseg, adr);
  322. ;   unsigned vseg;    /* the video segment address */
  323. ;   unsigned adr;     /* the video offset address  */
  324. ; ------------------------------------------
  325. _vpeek  proc
  326.         push    bp
  327.         mov     bp,sp
  328.         push    si
  329.         push    ds
  330.         mov     si,6[bp]    ; offset address
  331.         mov     cx,4[bp]    ; video board base address
  332.         mov     ds,cx
  333.         mov     dx,986      ; video status port address
  334. loop3:  in      al,dx       ; wait for retrace to stop
  335.         test    al,1
  336.         jnz     loop3
  337. loop4:  in      al,dx       ; wait for retrace to start
  338.         test    al,1
  339.         jz      loop4
  340.         lodsw               ; get the word
  341.         pop     ds
  342.         pop     si
  343.         pop     bp
  344.         ret
  345. _vpeek  endp
  346.         end
  347.  
  348.  
  349. [LISTING FOUR]
  350.  
  351. #
  352. #  TWRP.MAK -- make file for TWRP.EXE with Microsoft C/MASM
  353. #
  354.  
  355. .c.obj:
  356.     cl /DCOMPILER=MSOFT -c -W3 -Gs -AC $*.c
  357.  
  358. twrp.obj : twrp.c editor.h help.h window.h
  359.  
  360. editshel.obj : editshel.c editor.h menu.h entry.h help.h \
  361.                 window.h microsft.h
  362.  
  363. editor.obj : editor.c editor.h window.h microsft.h
  364.  
  365. entry.obj : entry.c entry.h window.h microsft.h
  366.  
  367. menu.obj : menu.c menu.h window.h microsft.h
  368.  
  369. help.obj : help.c help.h window.h microsft.h
  370.  
  371. window.obj : window.c window.h microsft.h
  372.  
  373. microsft.obj : microsft.c
  374.  
  375. vpeek.obj : vpeek.asm
  376.     masm /MX vpeek;
  377.  
  378. twrp.exe : twrp.obj editshel.obj editor.obj entry.obj menu.obj help.obj \
  379.         window.obj microsft.obj vpeek.obj
  380.     cl twrp editshel editor entry menu help window microsft vpeek
  381.  
  382.  
  383.